package com.cskaoyan.service;

import com.cskaoyan.mapper.MarketKeywordMapper;

import com.cskaoyan.module.bean.vo.MallKeywordVO;
import com.cskaoyan.module.pojo.MarketKeyword;
import com.cskaoyan.module.pojo.MarketKeywordExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author :ligenghuisheng
 * @Description:
 * @Date :2022/4/7  23:16
 */
@Service
public class MallKeywordServiceImpl implements MallKeywordService {

    @Autowired
    MarketKeywordMapper marketKeywordMapper;
    @Override
    public MallKeywordVO list(MallKeywordVO mallKeywordVO) {
        PageHelper.startPage(mallKeywordVO.getPage(),mallKeywordVO.getLimit());

        MarketKeywordExample marketKeywordExample = new MarketKeywordExample();

        MarketKeywordExample.Criteria criteria = marketKeywordExample.createCriteria();

        PageInfo<MarketKeyword> pageInfo = null;
        List<MarketKeyword> marketKeywords1 = null;
        List<MarketKeyword> marketKeywords2 = null;
        List<MarketKeyword> marketKeywords3 = null;
        if(mallKeywordVO.getKeyword()!= null ){
            criteria.andKeywordLike("%"+ mallKeywordVO.getKeyword()+"%");
            marketKeywords2 = marketKeywordMapper.selectByExample(marketKeywordExample);
            mallKeywordVO.setList(marketKeywords2);
            pageInfo = new PageInfo<>(marketKeywords2);




        }
        if(mallKeywordVO.getUrl()!= null ){
            criteria.andUrlLike("%"+ mallKeywordVO.getUrl()+"%");
            marketKeywords3 = marketKeywordMapper.selectByExample(marketKeywordExample);
            mallKeywordVO.setList(marketKeywords3);
            pageInfo = new PageInfo<>(marketKeywords3);
        }

        else {
            marketKeywords1 = marketKeywordMapper.selectByExample(marketKeywordExample);
            mallKeywordVO.setList(marketKeywords1);
            pageInfo = new PageInfo<>(marketKeywords1);
        }

        int pageSize = pageInfo.getPageSize();
        long total = pageInfo.getTotal();

        mallKeywordVO.setPage(mallKeywordVO.getPage());
        mallKeywordVO.setLimit(mallKeywordVO.getLimit());
        mallKeywordVO.setPages(pageSize);
        mallKeywordVO.setTotal((int) total);



        return mallKeywordVO;
    }

    @Override
    public MarketKeyword create(MarketKeyword marketKeyword) {
        marketKeyword.setSortOrder(50);
        int i = marketKeywordMapper.insert(marketKeyword);
        Integer id = marketKeyword.getId();
        MarketKeyword marketKeyword1 = marketKeywordMapper.selectByPrimaryKey(id);
        return marketKeyword1;
    }

    @Override
    public MarketKeyword update(MarketKeyword marketKeyword) {

        marketKeywordMapper.updateByPrimaryKey(marketKeyword);
        return marketKeyword;
    }

    @Override
    public void delete(MarketKeyword marketKeyword) {
        marketKeywordMapper.deleteByPrimaryKey(marketKeyword.getId());
    }
}
